package cn.kuporo.seckill.order.application.service.impl;

import cn.kuporo.seckill.common.exception.ErrorCode;
import cn.kuporo.seckill.common.exception.SeckillException;
import cn.kuporo.seckill.order.application.model.command.SeckillOrderCommand;
import cn.kuporo.seckill.common.model.dto.SeckillGoodsDTO;
import cn.kuporo.seckill.dubbo.interfaces.goods.SeckillGoodsDubboService;
import cn.kuporo.seckill.order.application.place.SeckillPlaceOrderService;
import cn.kuporo.seckill.order.application.security.SecurityService;
import cn.kuporo.seckill.order.application.service.SeckillSubmitOrderService;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;

/**
 * @author: kuporo
 * @since: 2025/7/11
 * @description: 提交订单基础实现类
 */
public abstract class SeckillBaseSubmitOrderServiceImpl implements SeckillSubmitOrderService {

    @DubboReference
    private SeckillGoodsDubboService seckillGoodsDubboService;
    @Resource
    protected SeckillPlaceOrderService seckillPlaceOrderService;
    @Resource
    private SecurityService securityService;

    @Override
    public void checkSeckillOrder(Long userId, SeckillOrderCommand seckillOrderCommand) {
        if (userId == null || seckillOrderCommand == null){
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        //模拟风控
        if (!securityService.securityPolicy(userId)){
            throw new SeckillException(ErrorCode.USER_INVALID);
        }
        //获取商品信息
        SeckillGoodsDTO seckillGoodsDTO = seckillGoodsDubboService.getSeckillGoods(seckillOrderCommand.getGoodsId(), seckillOrderCommand.getVersion());
        //检测商品信息
        seckillPlaceOrderService.checkSeckillGoods(seckillOrderCommand, seckillGoodsDTO);
    }
}
